package dryven.unittest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import dryven.unittest.models.shop.Client;
import dryven.unittest.models.shop.Product;
import dryven.util.ArrayIterable;
import dryven.view.ViewData;
import dryven.view.engine.expression.nodelisters.DefaultNodeListerFactory;
import dryven.view.engine.parser.CompilationException;
import dryven.view.engine.parser.CompiledViewFile;
import dryven.view.engine.parser.ViewFileCompiler;

import junit.framework.TestCase;

public class ViewCompilerTestCase extends TestCase {
	public void testFoo() throws CompilationException, IOException {
		InputStream stream = getClass().getResourceAsStream("/dryven/unittest/view/pages/page.xml");
		String packages[] = new String[]{"java.lang","dryven.unittest.models.shop"};
		ViewFileCompiler compiler = new ViewFileCompiler(stream, new DefaultNodeListerFactory(), new ArrayIterable<String>(packages));
		CompiledViewFile file = compiler.compile();
		
		Client c1 = new Client();
		c1.setName("Dragados");
		c1.setSponser(true);
		Client c2 = new Client();
		c2.setName("Accionistas");
		
		Client[] clients = new Client[]{c1,c2};
		Product product = new Product();
		product.setName("Guantes");
		
		ViewData data = new ViewData()
			.set("clients", clients)
			.set("title", "Dit is een test")
			.set("product", product);
		
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		Writer out = new OutputStreamWriter(outStream, "UTF-8");
		
		file.render(data, out);
		
		System.out.println(new String(outStream.toByteArray()));
	}
}
